package main

import "fmt"

func traverseArray() {
	arr := [...]int{1, 2, 3, 11, 12, 13, 30}
	for _, v := range arr {
		fmt.Print(v, " ")
	}
	fmt.Println("")
	for _, v := range arr {
		fmt.Print(v, " ")
	}
}

func mapForLoop() {
	ages := map[string]int{
		"alice":   31,
		"charlie": 34,
	}
	for name, age := range ages {
		fmt.Printf("%s\t%d\n", name, age)
	}
	for name := range ages {
		fmt.Printf("%s\n", name)
	}
	for _, age := range ages {
		fmt.Printf("%d\n", age)
	}
}

// map中元素的迭代顺序是不固定的。
func mapForLoopOrderTest() {
	myMap := map[string]int{
		"apple":  5,
		"banana": 3,
		"cherry": 7,
	}

	fmt.Println("第一次遍历：")
	for key, value := range myMap {
		fmt.Printf("Key: %s, Value: %d\n", key, value)
	}
	fmt.Println("第二次遍历：")
	for key, value := range myMap {
		fmt.Printf("Key: %s, Value: %d\n", key, value)
	}
	fmt.Println("第三次遍历：")
	for key, value := range myMap {
		fmt.Printf("Key: %s, Value: %d\n", key, value)
	}
	//一次执行结果：
	/*
		第一次遍历：
		Key: apple, Value: 5
		Key: banana, Value: 3
		Key: cherry, Value: 7
		第二次遍历：
		Key: apple, Value: 5
		Key: banana, Value: 3
		Key: cherry, Value: 7
		第三次遍历：
		Key: banana, Value: 3
		Key: cherry, Value: 7
		Key: apple, Value: 5
	*/
}
